home *** CD-ROM | disk | FTP | other *** search
- #pragma once
-
- #ifndef NDEBUG
-
- /* types of assertions */
- enum {
- ASSERT_REQUIRE_KIND, /* precondition */
- ASSERT_ENSURE_KIND, /* postcondition */
- ASSERT_CHECK_KIND, /* checkpoint */
- ASSERT_LAST_KIND
- };
-
- /* assert option flags control the behavior of assertions */
- struct _assert_options {
- Boolean require:1; /* true enables preconditions */
- Boolean ensure:1; /* true enables postconditions */
- Boolean check:1; /* true enables checkpoints */
- Boolean debug:1; /* true enters debugger on failure */
- Boolean raise:1; /* true raises exception on failure */
- Boolean print:1; /* true prints string on failure */
- Boolean stack:1; /* true prints call stack on failure */
- Boolean trace:1; /* true traces assertions */
- };
-
- extern struct _assert_options _assert;
-
- int _assert_failed(int type, const char *expr, const char *file, long line);
- int _assert_trace(int type, const char *expr, const char *file, long line);
-
- /* enable assertions by default */
- #ifndef ASSERT_REQUIRE
- #define ASSERT_REQUIRE (1)
- #endif
- #ifndef ASSERT_CHECK
- #define ASSERT_CHECK (1)
- #endif
- #ifndef ASSERT_ENSURE
- #define ASSERT_ENSURE (1)
- #endif
- #ifndef ASSERT_COMPACT
- #define ASSERT_COMPACT (1)
- #endif
-
- #ifdef THINK_C
- /* THINK C lets us declare a static variable in a prefix prepended to
- all compiled source files. This saves data space by removing
- multiple instances of the string __FILE__. */
- #define ASSERT_FILE (_assert_file)
- #else
- #define ASSERT_FILE __FILE__
- #endif
-
- #if ASSERT_COMPACT
- /* To save string data space we don't pass the stringified assert
- condition. Since the file and line number are still displayed
- by the assertion handling function we can still locate the
- assertion that failed in the source file. */
- #define ASSERT_CONDSTR NULL
- #else
- #define ASSERT_CONDSTR #cond
- #endif
-
- /* assertion macros */
-
- #if ASSERT_TRACE
- #define _assert_trace_macro(cond, flag, type) \
- ((flag) && _assert.trace ? \
- _assert_trace(type, ASSERT_CONDSTR, ASSERT_FILE, __LINE__) : 0)
- #else
- #define _assert_trace_macro(cond, flag, type) (0)
- #endif
-
- #define _assert_check(cond, flag, type) \
- ((void) ((flag) && ! (cond) ? \
- _assert_failed(type, ASSERT_CONDSTR, ASSERT_FILE, __LINE__) : \
- _assert_trace_macro(cond, flag, type)))
-
- #if ASSERT_REQUIRE
- #define require(cond) _assert_check(cond, _assert.require, ASSERT_REQUIRE_KIND)
- #else
- #define require(cond) ((void) 0)
- #endif
-
- #if ASSERT_ENSURE
- #define ensure(cond) _assert_check(cond, _assert.ensure, ASSERT_ENSURE_KIND)
- #else
- #define ensure(cond) ((void) 0)
- #endif
-
- #if ASSERT_CHECK
- #define check(cond) _assert_check(cond, _assert.check, ASSERT_CHECK_KIND)
- #else
- #define check(x) ((void) 0)
- #endif
-
- #else /* NDEBUG */
-
- #define require(cond) ((void) 0)
- #define ensure(cond) ((void) 0)
- #define check(x) ((void) 0)
-
- #endif /* NDEBUG */
-